﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.Views
{
    /// <summary>
    /// 显示状态同步线程
    /// </summary>
    public class SyncViewStateThread
    {
        private static bool s_isRunning = false;
        private static Thread s_syncThread;
        private static Coolzon.ViewState.ViewState s_destViewState;
        private static RenderView s_renderView;
        private static object s_lockObj = new object();

        /// <summary>
        /// 判断同步视图状态的线程是否正在运行
        /// </summary>
        /// <returns>true:正在运行中,false:没有再运行</returns>
        public static bool IsRunning()
        {
            return s_isRunning;
        }

        /// <summary>
        /// 启动同步线程，如果已经启动，那么什么都不会做。
        /// 同步之后，RenderView里的视图状态信息也会被更新。
        /// </summary>
        /// <param name="renderView"></param>
        /// <param name="destViewStateToken"></param>
        public static void Start(RenderView renderView, string destViewStateToken)
        {
            if (!s_isRunning)
            {
                lock (s_lockObj)
                {
                    if (!s_isRunning)
                    {
                        s_renderView = renderView;
                        s_destViewState = Coolzon.ViewState.ViewState.FromToken(destViewStateToken);
                        s_syncThread = new Thread(Running);
                        s_syncThread.Name = "SyncViewStateThread";
                        s_isRunning = true;
                        s_syncThread.Start();
                    }
                }
            }
        }
        private static void Running()
        {
            if (s_destViewState.CurrentScene != null)
            {
                // 通知服务器终端在进行同步
                Coolzon.Event.NCL.NCLUpdateClientStatusEvent @eventSyncing = new Coolzon.Event.NCL.NCLUpdateClientStatusEvent(Service.Contract.ClientStatus.Syncing);
                Coolzon.Event.NCL.NCLEventBus.PublishAsync(@eventSyncing);

                Coolzon.ViewState.ScenePath pathNode = s_destViewState.CurrentScene.GetRoot();
                while (pathNode != null)
                {
                    Coolzon.UI.Data.OpenSceneArgs openArgs = Coolzon.UI.Data.OpenSceneArgs.BuildFromString(pathNode.Arguments);
                    // 同步视图状态的时候需要将参数里的清空视图状态参数去除
                    if (openArgs.ParametersString.ContainsKey("sys:isresetviewstate"))
                    {
                        openArgs.ParametersString.Remove("sys:isresetviewstate");
                    }

                    if (string.Equals(pathNode.OpenMode, "open", StringComparison.CurrentCultureIgnoreCase))
                    {
                        s_renderView.OpenScene(Data.SwitchMode.Replace, "", "", pathNode.SceneName, openArgs);
                    }
                    else
                    {
                        s_renderView.OpenScene(Data.SwitchMode.Pop, "", "", pathNode.SceneName, openArgs);
                    }

                    pathNode = s_destViewState.CurrentScene.GetChild(pathNode);
                    if (pathNode != null)
                    {
                        Thread.Sleep(3000);
                    }
                }
                s_renderView.SetViewState(s_destViewState);

                // 通知服务器终端处于正常状态
                Coolzon.Event.NCL.NCLUpdateClientStatusEvent @eventNormal = new Coolzon.Event.NCL.NCLUpdateClientStatusEvent(Service.Contract.ClientStatus.Normal);
                Coolzon.Event.NCL.NCLEventBus.PublishAsync(@eventNormal);
            }
            else
            {
                Common.Logger.ErrorToTag(Library.LogTag, "接收到同步视图状态的回应，但是viewstate里没有当前打开的场景无法执行");
            }
            s_syncThread = null;
            s_isRunning = false;
            s_renderView = null;
        }
    }
}
